package com.cww.study.utils;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.util.LangUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @program: myes
 * @description:
 * @author: 陈英俊
 * @create: 2021-09-17 15:53
 */
public class StringUtil {
    private static final Logger logger = LoggerFactory.getLogger(StringUtil.class);

    private StringUtil() {
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0 || "null".equals(str);
    }

    public static boolean isEmpty(String... strs) {
        if (strs != null && strs.length >= 1) {
            String[] var1 = strs;
            int var2 = strs.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                String str = var1[var3];
                if (isNotEmpty(str)) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isNotEmpty(String... strs) {
        if (strs != null && strs.length >= 1) {
            String[] var1 = strs;
            int var2 = strs.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                String str = var1[var3];
                if (isEmpty(str)) {
                    return false;
                }
            }

            return true;
        } else {
            return false;
        }
    }

    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        if (length < 1) {
            length = 1;
        }

        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        for(int i = 0; i < length; ++i) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }

        return sb.toString();
    }

    public static String cutPreAndLowerFirst(String str, int preLength) {
        if (str == null) {
            return null;
        } else if (str.length() > preLength) {
            char first = Character.toLowerCase(str.charAt(preLength));
            return str.length() > preLength + 1 ? first + str.substring(preLength + 1) : String.valueOf(first);
        } else {
            return null;
        }
    }

    public static String upperFirstAndAddPre(String str, String preString) {
        return str != null && preString != null ? preString + Character.toUpperCase(str.charAt(0)) + str.substring(1) : null;
    }

    public static List<String> split(String str, char separator) {
        return split(str, separator, 0);
    }

    public static List<String> split(String str, char separator, int limit) {
        if (str == null) {
            return Collections.emptyList();
        } else {
            List<String> list = new ArrayList(limit == 0 ? 16 : limit);
            if (limit == 1) {
                list.add(str);
                return list;
            } else {
                boolean isNotEnd = true;
                int strLen = str.length();
                StringBuilder sb = new StringBuilder(strLen);

                for(int i = 0; i < strLen; ++i) {
                    char c = str.charAt(i);
                    if (isNotEnd && c == separator) {
                        list.add(sb.toString());
                        sb.delete(0, sb.length());
                        if (limit != 0 && list.size() == limit - 1) {
                            isNotEnd = false;
                        }
                    } else {
                        sb.append(c);
                    }
                }

                list.add(sb.toString());
                return list;
            }
        }
    }

    public static String repeat(char c, int count) {
        char[] result = new char[count];

        for(int i = 0; i < count; ++i) {
            result[i] = c;
        }

        return new String(result);
    }

    public static String transCharset(String str, String sourceCharset, String destCharset) {
        if (!LangUtil.isEmpty(str) && !LangUtil.isEmpty(sourceCharset) && !LangUtil.isEmpty(destCharset)) {
            try {
                return new String(str.getBytes(sourceCharset), destCharset);
            } catch (UnsupportedEncodingException var4) {
                return str;
            }
        } else {
            return str;
        }
    }

    public static String trim(String str, int specialCharsLength) {
        if (str != null && !"".equals(str) && specialCharsLength >= 1) {
            char[] chars = str.toCharArray();
            int charsLength = getCharsLength(chars, specialCharsLength);
            return new String(chars, 0, charsLength);
        } else {
            return "";
        }
    }

    private static int getCharsLength(char[] chars, int specialCharsLength) {
        int count = 0;
        int normalCharsLength = 0;

        for(int i = 0; i < chars.length; ++i) {
            int specialCharLength = getSpecialCharLength(chars[i]);
            if (count > specialCharsLength - specialCharLength) {
                break;
            }

            count += specialCharLength;
            ++normalCharsLength;
        }

        return normalCharsLength;
    }

    private static int getSpecialCharLength(char charStr) {
        return isLetter(charStr) ? 1 : 2;
    }

    private static boolean isLetter(char charStr) {
        int k = 128;
        return charStr / k == 0;
    }

    public static int length(String str) {
        if (str == null) {
            return 0;
        } else {
            char[] c = str.toCharArray();
            int len = 0;

            for(int i = 0; i < c.length; ++i) {
                ++len;
                if (!isLetter(c[i])) {
                    ++len;
                }
            }

            return len;
        }
    }

    public static String replaceEx(String str, String subStr, String reStr) {
        if (str == null) {
            return null;
        } else if (subStr != null && !subStr.equals("") && subStr.length() <= str.length() && reStr != null) {
            StringBuilder sb = new StringBuilder();
            int lastIndex = 0;

            while(true) {
                int index = str.indexOf(subStr, lastIndex);
                if (index < 0) {
                    sb.append(str.substring(lastIndex));
                    return sb.toString();
                }

                sb.append(str, lastIndex, index);
                sb.append(reStr);
                lastIndex = index + subStr.length();
            }
        } else {
            return str;
        }
    }

    public static String javaEncode(String txt) {
        if (txt != null && txt.length() != 0) {
            txt = replaceEx(txt, "\\", "\\\\");
            txt = replaceEx(txt, "\r\n", "\n");
            txt = replaceEx(txt, "\r", "\\r");
            txt = replaceEx(txt, "\t", "\\t");
            txt = replaceEx(txt, "\n", "\\n");
            txt = replaceEx(txt, "\"", "\\\"");
            txt = replaceEx(txt, "'", "\\'");
            return txt;
        } else {
            return txt;
        }
    }

    public static String leftPad(String srcString, char c, int length) {
        if (srcString == null) {
            srcString = "";
        }

        int tLen = srcString.length();
        if (tLen >= length) {
            return srcString;
        } else {
            int iMax = length - tLen;
            StringBuilder sb = new StringBuilder();

            for(int i = 0; i < iMax; ++i) {
                sb.append(c);
            }

            sb.append(srcString);
            return sb.toString();
        }
    }

    public static List<String> toList(String str) {
        List<String> resultList = new ArrayList();
        if (StringUtils.isBlank(str)) {
            return resultList;
        } else {
            String[] array = StringUtils.split(str, ",");
            String[] var3 = array;
            int var4 = array.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String element = var3[var5];
                if (StringUtils.isNotBlank(element)) {
                    resultList.add(element);
                }
            }

            return resultList;
        }
    }

    public static String listToString(List<String> list, String separator) {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < list.size(); ++i) {
            sb.append(list.get(i));
            if (i < list.size() - 1) {
                sb.append(separator);
            }
        }

        return sb.toString();
    }

    public static String increase(String value) {
        int index = 1;
        int n = Integer.parseInt(value.substring(index)) + 1;
        String newValue = String.valueOf(n);
        int len = value.length() - newValue.length() - index;

        for(int i = 0; i < len; ++i) {
            newValue = "0" + newValue;
        }

        return value.substring(0, index) + newValue;
    }

    public static String formattDecimal(double decimal) {
        DecimalFormat df = new DecimalFormat("######0.0000");
        return df.format(decimal);
    }

    public static String getParamValue(Object o, String param) {
        Object value = new Object();
        Method method = null;

        try {
            String methodName = param.substring(0, 1).toUpperCase() + param.substring(1);
            method = o.getClass().getMethod("get" + methodName);
            value = method.invoke(o);
            if (null == value) {
                return null;
            }
        } catch (Exception var5) {
            logger.error("", var5);
        }

        return value.toString();
    }

    public static void setParamValue(Object o, String param, String value) {
        Method method = null;

        try {
            String methodName = param.substring(0, 1).toUpperCase() + param.substring(1);
            method = o.getClass().getMethod("set" + methodName, String.class);
            method.invoke(o, value);
        } catch (Exception var5) {
            logger.error("", var5);
        }

    }

    public static String substring(String value, int begin, int length) {
        return value.substring(begin - 1, begin + length - 1);
    }

    public static boolean isBlank(String... strs) {
        if (strs != null && strs.length >= 1) {
            String[] var1 = strs;
            int var2 = strs.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                String str = var1[var3];
                if (isEmpty(str)) {
                    return true;
                }
            }

            return false;
        } else {
            return true;
        }
    }

    public static String camelCase(String underline) {
        StringBuilder camelCaseProperty = new StringBuilder();
        String[] underlineProperty = underline.split("_");

        for(int i = 0; i < underlineProperty.length; ++i) {
            if (i > 0) {
                Character initial = underlineProperty[i].charAt(0);
                String upperInitial = initial.toString().toUpperCase();
                String initialProperty = underlineProperty[i].replaceFirst(initial.toString(), upperInitial);
                camelCaseProperty.append(initialProperty);
            } else {
                camelCaseProperty.append(underlineProperty[i]);
            }
        }

        return camelCaseProperty.toString();
    }

    public static String camelLowerCase(String word) {
        StringBuilder camelCaseProperty = new StringBuilder();
        Character initial = word.charAt(0);
        String upperInitial = initial.toString().toLowerCase();
        String initialProperty = word.replaceFirst(initial.toString(), upperInitial);
        camelCaseProperty.append(initialProperty);
        return camelCaseProperty.toString();
    }
}
